/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"

#include "平面几何数据生成.h"

#include <面/几何模型生成.h>


template<typename T>
uvec2 f_vg_生成矩形顶点(std::vector<T>& vert) {
	uint32 beginOffset = vert.size();

	vert.push_back({ {0,0}, {0,0} });
	vert.push_back({ {1,0}, {1,0} });
	vert.push_back({ {1,1}, {1,1} });
	vert.push_back({ {0,1}, {0,1} });
	vert.push_back({ {0,0}, {0,0} });

	return { beginOffset, uint32(vert.size()) };
}

template uvec2 f_vg_生成矩形顶点(std::vector<S_2D图元顶点>& vert);   
template uvec2 f_vg_生成矩形顶点(std::vector<mat2>& vert);   


template<typename T>
uvec2 f_vg_生成方向圆角矩形顶点(std::vector<T>& vert, E_方向 方向) {
	uint32 beginOffset = vert.size();

	uint32 num = 6;
	std::vector<vec2> temp(num * 4);
	
	
	//======================= 生成全圆角矩形 =============================
	if (E_方向::e_中 == 方向) {
		f_graph_计算圆坐标XY({ 1.0, 1.0 }, 1.0, num * 4, temp.data());


		temp.push_back(temp[0]);
		vert.reserve(vert.size() + (num+1) * 4);

		for (uint32 i = num * 3; i <= num * 4; ++i) {
			vert.push_back({ temp[i] - vec2{1.0f, 0}, {1,0} });
		}
		for (uint32 i = 0; i <= num; ++i) {
			vert.push_back({ temp[i] - vec2{1.0f, 1.0f}, {1,1} });
		}
		for (uint32 i = num; i <= num * 2; ++i) {
			vert.push_back({ temp[i] - vec2{0, 1.0f}, {0,1} });
		}
		for (uint32 i = num * 2; i <= num * 3; ++i) {
			vert.push_back({ temp[i], {0,0} });
		}
		vert.push_back({ temp[num * 3] - vec2{1.0f, 0}, {1,0} });

		return { beginOffset, uint32(vert.size()) };
	}


	num = 6;
	temp.resize(num * 4);
	f_graph_计算圆坐标XY({ 1.0, 1.0 }, 1.0, num * 4, temp.data());


	//============================= 生成各个方向上的圆角矩形 ==============================
	vert.reserve(vert.size() + num * 2 + 2);
	switch (方向) {
		case E_方向::e_左: {
			//================== 左边 =====================
			//第三现象
			for (uint32 i = num; i <= num * 2; ++i) {
				vert.push_back({ temp[i] - vec2{0, 1.0f}, {0,1} });
			}
			//第二象限
			for (uint32 i = num * 2; i <= num * 3; ++i) {
				vert.push_back({ temp[i], {0,0} });
			}

			vert.push_back({ vec2{1.0f, 0.0f}, {1,0} });
			vert.push_back({ vec2{1.0f, 1.0f}, {1,1} });

			vert.push_back({ temp[num] - vec2{0, 1.0f}, {0,1} });
			break;
		}
		case E_方向::e_右: {
			//================= 右边 ====================
			//第四象限
			for (uint32 i = 0; i <= num; ++i) {
				vert.push_back({ temp[i] - vec2{1.0f, 1.0f}, {1,1} });
			}

			vert.push_back({ vec2{0.0f, 1.0f}, {0,1} });
			vert.push_back({ {0.0f, 0.0f}, {0,0} });

			//第一现象
			for (uint32 i = num * 3; i < num * 4; ++i) {
				vert.push_back({ temp[i] - vec2{1.0f, 0}, {1,0} });
			}
			vert.push_back({ temp[0] - vec2{1.0f, 1.0f}, {1,1} });
			break;
		}

		case E_方向::e_上: {
			//第二象限
			for (uint32 i = num * 2; i <= num * 3; ++i) {
				vert.push_back({ temp[i], {0,0} });
			}
			//第一现象
			for (uint32 i = num*3; i < num*4; ++i) {
				vert.push_back({ temp[i] - vec2{1.0f, 0}, {1,0} });
			}

			vert.push_back({ vec2{1.0f, 1.0f}, {1,1} });
			vert.push_back({ vec2{0.0f, 1.0f}, {0,1} });
			vert.push_back({ temp[num*2], {0,0} });
			break;
		}
		case E_方向::e_下: {
			//================= 右边 ====================
			//第四象限
			for (uint32 i = 0; i <= num; ++i) {
				vert.push_back({ temp[i] - vec2{1.0f, 1.0f}, {1,1} });
			}

			//第三现象
			for (uint32 i = num; i <= num * 2; ++i) {
				vert.push_back({ temp[i] - vec2{0, 1.0f}, {0,1} });
			}

			vert.push_back({ {0.0f, 0.0f}, {0,0} });
			vert.push_back({ vec2{1.0f, 0.0f}, {1,0} });
			vert.push_back({ temp[0] - vec2{1.0f, 1.0f}, {1,1} });
			break;
		}
		case E_方向::e_左上: {
			//第二象限
			for (uint32 i = num * 2; i <= num * 3; ++i) {
				vert.push_back({ temp[i], {0,0} });
			}
			vert.push_back({ vec2{1.0f, 0.0f}, {1,0} });
			vert.push_back({ vec2{1.0f, 1.0f}, {1,1} });
			vert.push_back({ vec2{0.0f, 1.0f}, {0,1} });
			break;
		}
		case E_方向::e_右上: {
			//第一现象
			for (uint32 i = num * 3; i < num * 4; ++i) {
				vert.push_back({ temp[i] - vec2{1.0f, 0}, {1,0} });
			}
			vert.push_back({ vec2{1.0f, 1.0f}, {1,1} });
			vert.push_back({ vec2{0.0f, 1.0f}, {0,1} });
			vert.push_back({ vec2{0.0f, 0.0f}, {0,0} });
			break;
		}
		default:
			break;
	}


	return { beginOffset, uint32(vert.size()) };
}
template uvec2 f_vg_生成方向圆角矩形顶点(std::vector<S_2D图元顶点>& vert, E_方向 方向);   
template uvec2 f_vg_生成方向圆角矩形顶点(std::vector<mat2>& vert, E_方向 方向);   


uvec2 f_vg_生成圆角连接线顶点(std::vector<S_2D图元顶点>& vert, E_方向 方向) {
	uint32 beginOffset = vert.size();

	uint32 num = 10;
	std::vector<vec2> temp(num * 4);
	f_graph_计算圆坐标XY({ 0.5, 0.5 }, 0.5, num * 4, temp.data());

	//float32 圆弧缩放 = 0;
	switch (方向) {
		case E_方向::e_纵向: {
			vert.push_back({ {0, 0}, {0,0}, {} });

			//float32 跨度 = 0.3 / num;
			float32 上半部分占比 = 0.02;

			vert.push_back({ {(1.0f - temp[0].x) * 0.4f,  (temp[0].y - 0.5f) * 0.4f}, {0, 0}, {0.1f,0.1f} });
			for (uint32 i = 0; i <= num; ++i) {
				vert.push_back({ {(1.0f-temp[i].x) * 0.4f,  (temp[i].y - 0.5f) * 0.4f}, {0, 上半部分占比}, {0.1f,0.1f} });
			}

			//跨度 = 0.7 / num;
			for (uint32 i = num*3; i < num*4; ++i) {
				vert.push_back({ {temp[i].x*1.6f - 0.6f, (temp[i].y + 0.5f) * 1.6f - 0.6f}, {1,上半部分占比}, {1.0f,1.0f} });
			}
			vert.push_back({ {1.0f, 1.0f}, {1,0.3f}, {1.0f,1.0f} });
			vert.push_back({ {1.0f, 1.0f}, {1,1}, {1.0f,1.0f} });

			break;
		}
		default: {
			float32 上半部分占比 = 0.4;
			float32 下半部分占比 = 1.0 - 上半部分占比;

			vert.push_back({ {0, 0}, {0,0}, {} });

			vert.push_back({ {(temp[30].x),  (temp[30].y)}, {0, 0}, {0.1f,0.1f} });
			for (uint32 i = 0; i < num; ++i) {
				vert.push_back({ {(temp[30+i].x-0.5f),  (temp[30+i].y)}, {0.5f, 0}, {0.1f,0.1f} });
			}

			for (uint32 i = 0; i < num; ++i) {
				vert.push_back({ {temp[20-i].x+0.5f, (temp[20-i].y)}, {0.5f,1}, {1.0f,1.0f} });
			}

			vert.push_back({ {1.0f, 1.0f}, {1,1}, {1.0f,1.0f} });
			vert.push_back({ {1.0f, 1.0f}, {1,1}, {1.0f,1.0f} });

			break;
		}
	}

	return { beginOffset, uint32(vert.size()) };
}

template<typename T>
uvec2 f_vg_生成直线顶点(std::vector<T>& vert, E_方向 方向) {
	uint32 beginOffset = vert.size();

	switch (方向) {
		case E_方向::e_横向:
			vert.push_back({ {0.0f, 0.0f}, {0,0} });
			vert.push_back({ {1.0f, 0.0f}, {1,0} });
			break;
		case E_方向::e_纵向:
			vert.push_back({ {0.0f, 0.0f}, {0,0} });
			vert.push_back({ {0.0f, 1.0f}, {0,1} });
			break;
		case E_方向::e_中:
			vert.push_back({ {0.0f, 0.0f}, {0,0} });
			vert.push_back({ {1.0f, 1.0f}, {1,1} });
			break;
	}
	
	return { beginOffset, uint32(vert.size()) };
}
template uvec2 f_vg_生成直线顶点(std::vector<S_2D图元顶点>& vert, E_方向 方向);
template uvec2 f_vg_生成直线顶点(std::vector<mat2>& vert, E_方向 方向);

uvec2 f_vg_生成C形拐角连接线顶点(std::vector<S_2D图元顶点>& vert, E_方向 方向) {
	uint32 beginOffset = vert.size();

	uint32 num = 10;
	std::vector<vec2> temp(num * 4);
	f_graph_计算圆坐标XY({ 0.5, 0.5 }, 0.5, num * 4, temp.data());


	float32 圆弧缩放 = 0;

	switch (方向) {
		case E_方向::e_左: {
			vert.push_back({ {1, 1}, {1,1}, {} });

			for (uint32 i = num; i < num * 2; ++i) {
				vert.push_back({ temp[i], { 0, 1.0f }, { 0.0f, 0.0f } });
			}

			for (uint32 i = num*2; i <= num*3; ++i) {
				vert.push_back({ temp[i], {0, 0}, {0.0f,0.0f}});
			}
		
			break;

		case E_方向::e_右: {


			break;
		}
		}
		default:
		break;
	}

	return { beginOffset, uint32(vert.size()) };
}

uvec2 f_vg_生成L形拐角连接线顶点(std::vector<S_2D图元顶点>& vert, E_方向 方向) {
	uint32 beginOffset = vert.size();

	uint32 num = 10;
	std::vector<vec2> temp(num * 4);
	f_graph_计算圆坐标XY({ 0.5, 0.5 }, 0.5, num * 4, temp.data());


	float32 圆弧缩放 = 0;

	switch (方向) {
		case E_方向::e_下: {
			vert.push_back({ {1, 1}, {1,1}, {} });

			for (uint32 i = num; i < num * 2; ++i) {
				vert.push_back({ temp[i], { 0, 1.0f }, { 0.0f, 0.0f } });
			}

			vert.push_back({ {0.0f, 0.0f}, {0,0}, {0.0f,0.0f} });
			break;

		case E_方向::e_上: {
			vert.push_back({ {1, 1}, {1,1}, {} });

			for (uint32 i = num * 2; i <= num * 3; ++i) {
				vert.push_back({ {-temp[i].x + 1.0f, temp[i].y}, {1, 0}, {0.0f,0.0f} });
			}

			vert.push_back({ {0.0f, 0.0f}, {0,0}, {0.0f,0.0f} });
			break;
		}
		}
		default:
			break;
	}

	return { beginOffset, uint32(vert.size()) };
}

template<typename T>
uvec2 f_vg_生成圆形顶点(std::vector<T>& vert) {
	uint32 beginOffset = vert.size();

	uint32 num = 64;
	std::vector<vec2> temp(num);
	f_graph_计算圆坐标XY({ 0.5, 0.5 }, 0.5, num, temp.data());

	for (uint32 i = 0; i < num; ++i) {
		vert.push_back({ temp[i], { temp[i].x, temp[i].y } });
	}

	return { beginOffset, uint32(vert.size()) };
}
template uvec2 f_vg_生成圆形顶点(std::vector<S_2D图元顶点>& vert);   
template uvec2 f_vg_生成圆形顶点(std::vector<mat2>& vert);   




